﻿using System;
using System.Collections.Generic;
using System.Text;
using WeChatRobotSDKCommon.Helper;
using WeChatRobotSDKModel.TableModel;

namespace WeChatRobotSDKServer
{
    public class StrProcessingServer
    {
        /// <summary>
        /// 文本消息处理
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string StrProcessing(Weibopp weibopp)
        {
            string text = weibopp.Text;
            string result = "";

            List<string> urls = new List<string>();

            string[] testStr = text.Split("<br />");

            foreach (var item in testStr)
            {
                string subResult = item;
                string urlstr = "";
                int i = 0;
                //寻找链接
                while (!string.IsNullOrEmpty(subResult))
                {
                    i += 1;
                    string resultText = StringHelper.GetSubString(subResult, "<a ", "</a>");
                    if (string.IsNullOrEmpty(resultText))
                    {
                        break;
                    }

                    string url = StringHelper.GetSubStringByLen(resultText, "data-url=\"", 20); ;
                    subResult = subResult.Replace("<a " + resultText + "</a>", "");
                    urlstr += $"\r链接：{url}";
                }
                if (string.IsNullOrEmpty(result))
                {
                    result += $"{subResult}{urlstr}";

                }
                else
                {
                    result += $"\r{subResult}{urlstr}";
                }
            }
            result += $"\r微博链接：{weibopp.WeiBoDetialUrl}";
            string titel = "";
            if (weibopp.YouHuiType == 0)
            {
                titel = "【线下活动优惠】";
            }
            else if (weibopp.YouHuiType == 1)
            {
                titel = "【京东优惠】";
            }
            if (weibopp.YouHuiType == 3)
            {
                titel = "【淘宝优惠】";
            }
            result = $"{titel}\r{result}";

            return result;

        }
    }
}
